{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SCIentific PYthon 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**`Ipython`** 提供了一个很好的解释器界面。\n",
    "\n",
    "**`Matplotlib`** 提供了一个类似 **`Matlab`** 的画图工具。\n",
    "\n",
    "**`Numpy`** 提供了 `ndarray` 对象，可以进行快速的向量化计算。\n",
    "\n",
    "**`Scipy`** 是 **`Python`** 中进行科学计算的一个第三方库，以 **`Numpy`** 为基础。\n",
    "\n",
    "**`Pandas`** 是处理时间序列数据的第三方库，提供一个类似 **`R`** 语言的环境。\n",
    "\n",
    "**`StatsModels`** 是一个统计库，着重于统计模型。\n",
    "\n",
    "**`Scikits`** 以 **`Scipy`** 为基础，提供如 **`scikits-learn` 机器学习**和**`scikits-image` 图像处理**等高级用法。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scipy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**`Scipy`** 由不同科学计算领域的子模块组成：\n",
    "\n",
    "子模块|描述\n",
    "----|----\n",
    "`cluster`|  聚类算法\n",
    "`constants`| 物理数学常数\n",
    "`fftpack`| 快速傅里叶变换\n",
    "`integrate`| 积分和常微分方程求解\n",
    "`interpolate`| 插值\n",
    "`io`| 输入输出\n",
    "`linalg`| 线性代数\n",
    "`odr`| 正交距离回归\n",
    "`optimize`| 优化和求根\n",
    "`signal`| 信号处理\n",
    "`sparse`| 稀疏矩阵\n",
    "`spatial`| 空间数据结构和算法\n",
    "`special`| 特殊方程\n",
    "`stats`| 统计分布和函数\n",
    "`weave`| C/C++ 积分\n",
    "\n",
    "在使用 **`Scipy`** 之前，为了方便，假定这些基础的模块已经被导入："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy as sp\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 **Scipy** 中的子模块时，需要分别导入："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from scipy import linalg, optimize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于一些常用的函数，这些在子模块中的函数可以在 `scipy` 命名空间中调用。另一方面，由于 **`Scipy`** 以 **`Numpy`** 为基础，因此很多基础的 **`Numpy`** 函数可以在`scipy` 命名空间中直接调用。\n",
    "\n",
    "我们可以使用 `numpy` 中的 `info` 函数来查看函数的文档："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None,\n",
      "      full_output=0, disp=1, retall=0, callback=None)\n",
      "\n",
      "Minimize a function using the downhill simplex algorithm.\n",
      "\n",
      "This algorithm only uses function values, not derivatives or second\n",
      "derivatives.\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "func : callable func(x,*args)\n",
      "    The objective function to be minimized.\n",
      "x0 : ndarray\n",
      "    Initial guess.\n",
      "args : tuple, optional\n",
      "    Extra arguments passed to func, i.e. ``f(x,*args)``.\n",
      "callback : callable, optional\n",
      "    Called after each iteration, as callback(xk), where xk is the\n",
      "    current parameter vector.\n",
      "xtol : float, optional\n",
      "    Relative error in xopt acceptable for convergence.\n",
      "ftol : number, optional\n",
      "    Relative error in func(xopt) acceptable for convergence.\n",
      "maxiter : int, optional\n",
      "    Maximum number of iterations to perform.\n",
      "maxfun : number, optional\n",
      "    Maximum number of function evaluations to make.\n",
      "full_output : bool, optional\n",
      "    Set to True if fopt and warnflag outputs are desired.\n",
      "disp : bool, optional\n",
      "    Set to True to print convergence messages.\n",
      "retall : bool, optional\n",
      "    Set to True to return list of solutions at each iteration.\n",
      "\n",
      "Returns\n",
      "-------\n",
      "xopt : ndarray\n",
      "    Parameter that minimizes function.\n",
      "fopt : float\n",
      "    Value of function at minimum: ``fopt = func(xopt)``.\n",
      "iter : int\n",
      "    Number of iterations performed.\n",
      "funcalls : int\n",
      "    Number of function calls made.\n",
      "warnflag : int\n",
      "    1 : Maximum number of function evaluations made.\n",
      "    2 : Maximum number of iterations reached.\n",
      "allvecs : list\n",
      "    Solution at each iteration.\n",
      "\n",
      "See also\n",
      "--------\n",
      "minimize: Interface to minimization algorithms for multivariate\n",
      "    functions. See the 'Nelder-Mead' `method` in particular.\n",
      "\n",
      "Notes\n",
      "-----\n",
      "Uses a Nelder-Mead simplex algorithm to find the minimum of function of\n",
      "one or more variables.\n",
      "\n",
      "This algorithm has a long history of successful use in applications.\n",
      "But it will usually be slower than an algorithm that uses first or\n",
      "second derivative information. In practice it can have poor\n",
      "performance in high-dimensional problems and is not robust to\n",
      "minimizing complicated functions. Additionally, there currently is no\n",
      "complete theory describing when the algorithm will successfully\n",
      "converge to the minimum, or how fast it will if it does.\n",
      "\n",
      "References\n",
      "----------\n",
      ".. [1] Nelder, J.A. and Mead, R. (1965), \"A simplex method for function\n",
      "       minimization\", The Computer Journal, 7, pp. 308-313\n",
      "\n",
      ".. [2] Wright, M.H. (1996), \"Direct Search Methods: Once Scorned, Now\n",
      "       Respectable\", in Numerical Analysis 1995, Proceedings of the\n",
      "       1995 Dundee Biennial Conference in Numerical Analysis, D.F.\n",
      "       Griffiths and G.A. Watson (Eds.), Addison Wesley Longman,\n",
      "       Harlow, UK, pp. 191-208.\n"
     ]
    }
   ],
   "source": [
    "np.info(optimize.fmin)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以用 `lookfor` 来查询特定关键词相关的函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Search results for 'resize array'\n",
      "---------------------------------\n",
      "numpy.chararray.resize\n",
      "    Change shape and size of array in-place.\n",
      "numpy.ma.resize\n",
      "    Return a new masked array with the specified size and shape.\n",
      "numpy.oldnumeric.ma.resize\n",
      "    The original array's total size can be any size.\n",
      "numpy.resize\n",
      "    Return a new array with the specified shape.\n",
      "numpy.chararray\n",
      "    chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0,\n",
      "numpy.memmap\n",
      "    Create a memory-map to an array stored in a *binary* file on disk.\n",
      "numpy.ma.mvoid.resize\n",
      "    .. warning::\n"
     ]
    }
   ],
   "source": [
    "np.lookfor(\"resize array\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还可以指定查找的模块："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Search results for 'remove path'\n",
      "--------------------------------\n",
      "os.removedirs\n",
      "    removedirs(path)\n",
      "os.walk\n",
      "    Directory tree generator.\n"
     ]
    }
   ],
   "source": [
    "np.lookfor(\"remove path\", module=\"os\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
